<?php

require_once("db.php");
connect_db("junyi", 'admin123', 'bookmark');

$code = $_REQUEST['code'];

if (!$code || !invitation_code_usable($code)) {
  echo "Your code ($code) is not usable, sorry Pal";
  return;
}

if (isset($_POST["username"]) && isset($_POST["password"])) {
   $username = mysql_real_escape_string($_POST["username"]);
   $password = mysql_real_escape_string($_POST["password"]);
   if (isset($_POST["email"])) {
     $email = mysql_real_escape_string($_POST["email"]);
   } else {
     $email = "";
   }
   $ret = register($code, $username, $password, $email);
   if ($ret == 0) {
     echo "Register finished. You can login now.";
     exit(0);
   }
   else if ($ret == 1) {
     $baduser = "user name is taken";
   } else if ($ret == 2) {
     $bademail = "email address is taken";
   } else if ($ret == 3) {
     $baduser = "user name or password too short, we are providing serious service, brother.";
   } else {
     die("bad register() return value:$ret");
   }
}


?>

<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

  <link rel="stylesheet" type="text/css" href="css/register.css" />
  <link rel="stylesheet" type="text/css" href="css/reset.css" />
  <link rel="stylesheet" type="text/css" href="css/modal.css" />
 
  <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
  <script type="text/javascript" src="js/simplemodal.js"></script>
  <script type="text/javascript" src="js/sprintf.js"></script>
  <title>Bookipedia | Register, with little effort</title>
</head>

<body>
  <form method="POST" action="register.php" id="signup-form">
    <h3>Welcome to Blackid, you can sign up now</h3>
    <label>Username:</label>
    <input type="text" name="username" value="<?=$username?>"/> <span class="msg"><?= $baduser ?></span>
    <label>Password:</label>
    <input type="password" name="password" />
    <label>Email(optional):</label>
    <input type="text" name="email" value="<?=$email?>" />
    <span class="msg"><?= $bademail ?></span>
    <input type="hidden" name="code" value="<?=$code?>"/>
    <button type="submit">submit</button>
  </form>
</body>

</html>
